<?php

//  retrieve_file.php
//  Memento 3.0
//
//  Created by Christopher Burrows on 10-07-29
//  Copyright 2010 Team Memento. All rights reserved.
//
//    Description:
//  Displays the file requested by retrieve.php. The former
//  script sends HTML POST data which indicate which file to
//  display. The Memeory data exist off-root so access to them
//  is protected by requiring session validation.
//
//  10-08-02: Fix typo preventing audio files from downloading.  (C. Maddocks)
//  10-07-29: File created. (C. Burrows)
//
//  HTML POST Fields:
//  'session_token' (string)
//  'collection_id' (int)
//  'id'            (int) [the memory index]
//  'component'     (int 0-2)
    
include ($_SERVER['DOCUMENT_ROOT'].'/functions.php');

connect_to_database();

$session_token = $_GET['session'];
$collection_id = $_GET['collection'];
$id = $_GET['id'];
$action = $_GET['action'];

if (validate_session_token($session_token) == true) {

    $downloaddir = '/home/mementof/memento.freehostingcloud.com/upload/'.$collection_id;
    $downloadfile = '/'.$id;
        
    if ($action == '1') {
        
        header('Content-Type: image/jpeg');
        header("Content-Disposition:attachment; filename=$collection_id-$id.jpg");
        echo file_get_contents ($downloaddir.$downloadfile.'.jpg');
        
    } else {
        
        header('Content-Type: audio/x-caf');
        header("Content-Disposition:attachment; filename=$collection_id-$id.caf");
        echo file_get_contents ($downloaddir.$downloadfile.'.caf');
        
    }

}
    
?>